/****************************************************
 * Description: ServiceImpl for 用户
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
    *  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuol.bfsuolframework.security.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bfsuol.bfsuolframework.core.controller.support.Pagination;
import com.bfsuol.bfsuolframework.security.dao.BaseUserMapper;
import com.bfsuol.bfsuolframework.security.dao.RoleMapper;
import com.bfsuol.bfsuolframework.security.dao.BaseUser;
import com.bfsuol.bfsuolframework.security.dao.Group;
import com.bfsuol.bfsuolframework.security.dao.Role;
import com.bfsuol.bfsuolframework.security.entity.dto.RoleDTO;
import com.bfsuol.bfsuolframework.security.service.BaseUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;


@Service
public class BaseUserServiceImpl extends ServiceImpl<BaseUserMapper,BaseUser> implements BaseUserService {

    @Autowired
    private BaseUserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;


	/* (non-Javadoc)
	 * @see BaseUserService#checkEmail(java.lang.String, java.lang.Long)
	 */
	public Boolean checkEmail(String email, Long id) {
		// TODO Auto-generated method stub
		//return this.checkEmail(email, id);
		return null;
	}

	public Boolean checkLoginName(String loginName, Long id) {
		// TODO Auto-generated method stub
		//return userMapper.checkLoginName(loginName, id);
		return null;
	}

	/* (non-Javadoc)
	 * @see BaseUserService#findUserByGroup(java.lang.String, java.lang.String, java.lang.String, java.lang.Long, com.bfsuolframework.core.controller.support.Pagination)
	 */
	@Override
	public Pagination findUserByGroup(String name, String loginName,
			String email, Long groupId, Pagination page,boolean isBelong) {
		
		/*page.setTotalRecord(userMapper.getUserCountByGroup(name,loginName,email,groupId,page,isBelong));
		page.setItems(userMapper.findUserByGroup(name,loginName,email,groupId,page,isBelong));*/
		return page;
	}


	/* (non-Javadoc)
	 * @see BaseUserService#addOrDelGroup(java.lang.Long, java.util.Collection, java.util.List)
	 */
	public String addOrDelGroup(Long userId, Collection<Role> roleOld,
			List<RoleDTO> roleDTOList) {
		/*List<Role> delRoleList = new ArrayList<Role>();// 返回 需要删除的角色集合
		List<Long> delLong = new ArrayList<Long>(); // 获得需要删除的角色ID集合
		List<Long> newLong = new ArrayList<Long>();// 获取新修改用户组下，可选择的角色集合
		
		if (roleDTOList != null && roleDTOList.size() > 0) {
			for (RoleDTO roleDTO : roleDTOList) {// 遍历 新用户组下的 所有角色
					newLong.add(roleDTO.getRole().getId());// 设置新的用户组，拥有的全部角色ID
			}
			
			if(roleOld!=null&&roleOld.size()>0){
				for (Role role : roleOld) {// 遍历 原已勾选角色集合
					delLong.add(role.getId());
				}
			}
			
			// 原有已勾选角色 而新的用户组下 没有的角色集合，即需要删除的角色
			delLong.removeAll(newLong);
			
			if (delLong != null && delLong.size() > 0) {
				for (Long id : delLong) {
					delRoleList.add(roleMapper.getById(id));
				}
			} else {
				delRoleList = null;
			}
		}
		
		BaseUser bu = userMapper.getById(userId);
		
		// 如果存在需要删除的角色关系
		if(delRoleList!=null&&delRoleList.size()>0){
			for(int i=0;i<delRoleList.size();i++){
				bu.removeRole(delRoleList.get(i));
			}
		}

		userMapper.update(bu);*/
		
		// 删除用户权限后刷新菜单缓存
		//SecurityContext.reloadRoleMenusFromDB();
		
		return "ok";
	}

	
	/* (non-Javadoc)
	 * @see BaseUserService#canChooseRoleList(java.lang.Long, java.util.Collection)
	 */
	public List<RoleDTO> canChooseRoleList(Long userId, Collection<Group> groups) {
		/*int i = 0;
		List<RoleDTO> roles = new ArrayList<RoleDTO>();// 返回的用户拥有的全部角色DTO集合
		List<Long> allDefault = new ArrayList<Long>();// 所有分组 默认的角色ID集合
		List<Long> allChoose = new ArrayList<Long>();// 所有分组 可选择的角色ID集合

		if (groups != null && groups.size() > 0) { // 数据正确性验证 大于一个分组的用户
			for (Group group : groups) {// 获取全部roleId
				List<Long> tempDefault = new ArrayList<Long>();
				List<Long> tempChoose = new ArrayList<Long>();
				for (GroupRole groupRole : group.getRoles()) {
					// 将roleId添加到temp
					if (groupRole.getIsDefault().intValue() == BfsuolConstants.GLOBAL_YESNO_YES.intValue()) {// 该组下默认的角色id
						tempDefault.add(groupRole.getRole().getId());
					}
					if (groupRole.getIsDefault().intValue() == 0 || groupRole.getIsDefault().intValue() == BfsuolConstants.GLOBAL_YESNO_NO.intValue()) {// 该组下可选的角色id
						tempChoose.add(groupRole.getRole().getId());
					}

				}
				if (i == 0) {// 获取第一组数据
					allDefault = tempDefault;
					allChoose = tempChoose;
					i++;
				} else {// 获取不重复的role
					allDefault.removeAll(tempDefault);
					allDefault.addAll(tempDefault);

					allChoose.removeAll(tempChoose);
					allChoose.addAll(tempChoose);
				}
			}

			// 去除组之间默认与可选重叠的role
			allChoose.removeAll(allDefault);

			// 根据id获取全部默认role对象
			for (Long id : allDefault) {
				RoleDTO roleDTO = new RoleDTO();
				Role role = roleMapper.getById(id);
				roleDTO.setRole(role);
				roleDTO.setIsDefault(0);
				roles.add(roleDTO);
			}

			if (userId != null) {
				BaseUser bu = userMapper.getById(userId);
				// 根据id获取全部可选role对象
				for (Long id : allChoose) {
					RoleDTO roleDTO = new RoleDTO();
					Role role = roleMapper.getById(id);
					roleDTO.setRole(role);
					// 从user_role表中获取已勾选role对象
					if (bu != null && bu.getRoles() != null
							&& bu.getRoles().size() > 0) {
						for (Role r : bu.getRoles()) {
							if (r.getId() == id) { // 已勾选过的角色
								roleDTO.setIsDefault(2);
							}
						}
					}

					if (roleDTO.getIsDefault() == null) { // 未勾选过的角色
						roleDTO.setIsDefault(1);
					}

					roles.add(roleDTO);
				}
			}
		}
		return roles;*/
		return null;
	}

	@Override
	public String[] getUserByLoginName(String loginName) {
		/*BaseUser baseUser = userMapper.getUserByLoginName(loginName);
		if(baseUser==null){
			return new String[10];
		}
		Collection<Role> roles = baseUser.getRoles();
		String[] roleCodes=new String[200];
		//List<String> list=new ArrayList<String>();
		Iterator<Role> iterator = roles.iterator();
		int i=0;
		while(iterator.hasNext()){
			Role r = iterator.next();
			if(r!=null){
				roleCodes[i]=r.getCode();
				i++;
			}
		}

		return roleCodes;*/
		return null;
	}
	/*
	 * (non-Javadoc)
	 * @see BaseUserService#changeStatus(java.lang.Long, java.lang.Integer)
	 */
	public void changeStatus(Long id, Integer status) {
		/*BaseUser baseUser = userMapper.getById(id);
		if(baseUser.getStatus()!=null&&Constants.STATUS_VALID.equals(baseUser.getStatus())){
			baseUser.setStatus(Constants.STATUS_INVALID);
			userMapper.update(baseUser);
		}else{
			baseUser.setStatus(Constants.STATUS_VALID);
			userMapper.update(baseUser);
		}*/
	}

	/*
	 * (non-Javadoc)
	 * @see BaseUserService#resetPwd(java.lang.Long)
	 */
	public void resetPwd(Long id){
		/*BaseUser baseUser = userMapper.getById(id);
		baseUser.setPassword(EncryptUtils.MD5Encode(Constants.STU_MOREN_PASSWORD));
		userMapper.update(baseUser);*/
	}
	/*
	 * (non-Javadoc)
	 * @see BaseUserService#registUser(BaseUser)
	 */
	public boolean registUser(BaseUser bu) {
		/*Long save = userMapper.save(bu);
		if(save!=null){
			return true;
		}else{
			return false;
		}*/
		return true;
		
	}
}